package com.zjmzxfzhl.modules.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.zjmzxfzhl.common.core.exception.SysException;
import com.zjmzxfzhl.modules.app.entity.AppProj;
import com.zjmzxfzhl.modules.app.entity.AppTask;
import com.zjmzxfzhl.modules.app.entity.AppTermUser;
import com.zjmzxfzhl.modules.app.form.AppProjUserVo;
import com.zjmzxfzhl.modules.app.service.AppProjService;
import com.zjmzxfzhl.modules.sys.entity.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zjmzxfzhl.common.core.base.BaseServiceImpl;
import com.zjmzxfzhl.modules.app.entity.AppProjUser;
import com.zjmzxfzhl.modules.app.mapper.AppProjUserMapper;
import com.zjmzxfzhl.modules.app.service.AppProjUserService;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * Service
 * 
 * @author 张森荣
 */
@Service
public class AppProjUserServiceImpl extends BaseServiceImpl<AppProjUserMapper, AppProjUser>
        implements AppProjUserService {
    @Autowired
    private AppProjService projService;

    @Override
    public IPage<AppProjUserVo> list(IPage<AppProjUserVo> page, AppProjUserVo appProjUserVo) {
        return page.setRecords(baseMapper.list(page, appProjUserVo));
    }

    @Override
    public List<AppProjUserVo> getList(IPage<AppProjUserVo> page, AppProjUserVo appProjUserVo) {
        return baseMapper.list(page, appProjUserVo);
    }

    /**
     * 查询所有项目成员编号
     * @return
     */
    public List<String> selectUserIdList(String projId){
        return baseMapper.selectUserIdList(projId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveAppProjUser(AppProjUser appProjUser) {
        Map<SFunction<AppProjUser,?>,Object> params=new HashMap<>();
        if(appProjUser.getProjId()!=null&&!"".equals(appProjUser.getProjId())){
            params.put(AppProjUser::getProjId,appProjUser.getProjId());
        }
        if(appProjUser.getUserId()!=null&&!"".equals(appProjUser.getUserId())){
            params.put(AppProjUser::getUserId,appProjUser.getUserId());
        }
        Integer total=this.count(new LambdaQueryWrapper<AppProjUser>().allEq(params));
        if(total>0){
            return false;
        }
        //判断是否存在非父项目成员
        AppProj appProj=projService.getById(appProjUser.getProjId());
        if (appProj.getParentProjId() != null&& !"".equals(appProj.getParentProjId())) {
            List<SysUser> userList=new ArrayList<>();
            SysUser sysUser=new SysUser();
            sysUser.setUserId(appProjUser.getUserId());
            userList.add(sysUser);
            if (!checkMembers(userList, appProj.getParentProjId())) {
                throw new SysException("存在不属于父项目的参与成员！");
            }
        }
        appProjUser.setId(UUID.randomUUID().toString().replace("-", "").toLowerCase());
        Boolean result=this.save(appProjUser);
        return result;
    }

    /**
     * 新增项目，自动计算机构级别、机构级次码、是否叶子
     *
     * @param appProjUser
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean relationDel(AppProjUser appProjUser) {
        Map<String, Object> columnMap=new HashMap<>();
        if (appProjUser.getProjId() == null && appProjUser.getUserId() == null) {
            return false;
        }
        if(appProjUser.getProjId()!=null){
            columnMap.put("PROJ_ID",appProjUser.getProjId());
        }
        if(appProjUser.getUserId()!=null){
            columnMap.put("USER_ID",appProjUser.getUserId());
        }
        Boolean result=this.removeByMap(columnMap);
        return result;
    }


    /**
     * 检查成员是否存在不属于项目的成员
     * @param userList
     * @param projId
     * @return
     */
    public boolean checkMembers(List<SysUser> userList, String projId){
        List<String> userIdList = new ArrayList<>();
        if(userList==null){
            return true;
        }
        for (SysUser user : userList) {
            userIdList.add(user.getUserId());
        }
        List<String> projUserIdList = selectUserIdList(projId);
        for (String i : userIdList) {
            if (!projUserIdList.contains(i)) {
                return false;
            }
        }
        return true;
    }

    public List<AppProjUserVo>  getOtherUserList(AppProjUserVo appProjUserVo){
        return baseMapper.getOtherUserList(appProjUserVo);
    }
}
